package player

import (
	"GameServer/Common/data"
	"GameServer/Common/utils"
	"GameServer/Game/dbmodule"
	"GameServer/Game/errors"
	"GameServer/Game/lib"
	"GameServer/Game/m"
	"GameServer/Game/sp"

	"github.com/globalsign/mgo/bson"
)

// 12010 获取facebookId对应玩家列表
func (p *Player) GetFriendsLstRequest(req *sp.GetFriendsLstRequest) (res *sp.GetFriendsLstResponse) {
	uFdata := p.User.GetUserFriendsData()
	// 填写返回
	uFdata = p.User.GetUserFriendsData()
	res = &sp.GetFriendsLstResponse{
		Errcode:       0,
		FriendsData:   lib.FormatUserFriendsData(uFdata),
		ReqFriendsLst: lib.FormatSimpleFriendsDat(uFdata.ReqFriendsLst),
	}
	return
}

// 12000 获取facebookId对应玩家列表
func (p *Player) GetFriendsLstByFBIdRequest(req *sp.GetFriendsLstByFBIdRequest) (res *sp.GetFriendsLstByFBIdResponse) {
	uFdata := p.User.GetUserFriendsData()
	uFLst_Current := uFdata.FriendsLst  // 玩家好友列表
	fbIdLst := req.FbIdLst              // 玩家现有FB好友
	deleteMap := make(map[string]int64) // 删除列表
	addMap := make(map[string]int64)    // 添加列表
	if fbIdLst != nil && len(fbIdLst) > 0 {
		// 玩家FB好友数量不为空
		fbFriendMap := make(map[string]int64)
		for _, theFBId := range fbIdLst {
			//fbFriendMap[uid] = 1
			// 根据fbId查找用户的UID
			var theU *m.User = new(m.User)
			err := dbmodule.MgoDB.GameDB.FindOne("user", bson.M{"faceBookId": theFBId}, nil, theU)
			if err == nil {
				fbFriendMap[theU.Uid.Hex()] = 1
			}
		}
		// 删除好友--遍历好友列表，发现不在FB好友列表中的人，删除
		for uid, data := range uFLst_Current {
			if data.FriendTyp == 1 {
				_, ok := fbFriendMap[uid]
				if ok == false {
					deleteMap[uid] = 1
				}
			}
		}
		// 添加好友--遍历FB好友列表，发现不在当前好友列表中的人，添加
		for uid, _ := range fbFriendMap {
			_, ok := uFLst_Current[uid]
			if ok == false {
				addMap[uid] = 1
			}
		}
	} else {
		// 玩家FB好友为空，删除所有标识为FB的好友
		for uid, data := range uFLst_Current {
			if data.FriendTyp == 1 {
				deleteMap[uid] = 1
			}
		}
	}
	// 执行删除
	if len(deleteMap) > 0 {
		for uid, _ := range deleteMap {
			uFdata.DeleteFriend(uid)
		}
	}
	// 执行添加
	if len(addMap) > 0 {
		for uid, _ := range addMap {
			lib.MakeFriendsWithPlayer(p.User, uid)
		}
	}
	// 填写返回
	uFdata = p.User.GetUserFriendsData()
	res = &sp.GetFriendsLstByFBIdResponse{
		Errcode:     0,
		FriendsData: lib.FormatUserFriendsData(uFdata),
	}
	return
}

// 12001 一键领取礼物，单个多个混合
func (p *Player) GetFriendsGiftRequest(req *sp.GetFriendsGiftRequest) (res *sp.GetFriendsGiftResponse) {
	if req.ReciveAll != 0 {
		// 全部礼物接收
		uProp, resGoods, boxResList, err := lib.GetFriendsGiftAll("GetFriendsGiftRequest", p.User, p.Send)
		if err != nil {
			res = &sp.GetFriendsGiftResponse{
				Errcode: errors.ErrCode(err),
			}
			return
		}

		// 有成就完成，进行通知
		achLst := lib.AchievComplete(p.User)
		if len(achLst) > 0 {
			p.Send(&sp.AchievementNoticeRequest{
				Lst: achLst,
			})
		}

		fData := p.User.GetUserFriendsData()
		res = &sp.GetFriendsGiftResponse{
			Errcode:     0,
			FriendsData: lib.FormatUserFriendsData(fData),
			Rewards:     lib.FormatGoodsInfoSps(resGoods),
			BoxRes:      lib.FormatBoxResSps(boxResList),
		}
		p.TimelyUpdateProp(req.GetType(), uProp)
		utils.AsyncFunc(func() {
			p.UpdateProp(req.GetType(), uProp)
		})
		return
	} else {
		// 接收单个UID的礼物
		if req.UidLst == "" {
			err := errors.NewErrcode(data.Errcode.ServerErr, "接收对象UID为空")
			res = &sp.GetFriendsGiftResponse{
				Errcode: errors.ErrCode(err),
			}
			return
		} else {
			uProp, resGoods, boxResList, err := lib.GetFriendsGiftByUID("GetFriendsGiftRequest", p.User, req.UidLst, p.Send)
			if err != nil {
				res = &sp.GetFriendsGiftResponse{
					Errcode: errors.ErrCode(err),
				}
				return
			}

			// 有成就完成，进行通知
			achLst := lib.AchievComplete(p.User)
			if len(achLst) > 0 {
				p.Send(&sp.AchievementNoticeRequest{
					Lst: achLst,
				})
			}

			fData := p.User.GetUserFriendsData()
			res = &sp.GetFriendsGiftResponse{
				Errcode:     0,
				FriendsData: lib.FormatUserFriendsData(fData),
				Rewards:     lib.FormatGoodsInfoSps(resGoods),
				BoxRes:      lib.FormatBoxResSps(boxResList),
			}
			p.TimelyUpdateProp(req.GetType(), uProp)
			utils.AsyncFunc(func() {
				p.UpdateProp(req.GetType(), uProp)
			})
			return
		}
	}
}

// 12002 一键赠送礼物
func (p *Player) SendFriendsGiftRequest(req *sp.SendFriendsGiftRequest) (res *sp.SendFriendsGiftResponse) {
	var sendGiftNum int64 = 1
	if req.SendAll != 0 {
		// 全部好友赠送
		lib.SendGiftToFriendAll(p.User, sendGiftNum)

		// 有成就完成，进行通知
		achLst := lib.AchievComplete(p.User)
		if len(achLst) > 0 {
			p.Send(&sp.AchievementNoticeRequest{
				Lst: achLst,
			})
		}
		fData := p.User.GetUserFriendsData()
		res = &sp.SendFriendsGiftResponse{
			Errcode:     0,
			FriendsData: lib.FormatUserFriendsData(fData),
		}
		return
	} else {
		// 赠送单个好友
		if req.UidLst == "" {
			err := errors.NewErrcode(data.Errcode.ServerErr, "赠送对象UID为空")
			res = &sp.SendFriendsGiftResponse{
				Errcode: errors.ErrCode(err),
			}
			return
		} else {
			lib.SendGiftToFriend(p.User, req.UidLst, sendGiftNum)

			// 有成就完成，进行通知
			achLst := lib.AchievComplete(p.User)
			if len(achLst) > 0 {
				p.Send(&sp.AchievementNoticeRequest{
					Lst: achLst,
				})
			}
			fData := p.User.GetUserFriendsData()
			res = &sp.SendFriendsGiftResponse{
				Errcode:     0,
				FriendsData: lib.FormatUserFriendsData(fData),
			}
			return
		}
	}
}

// 12005 强制交朋友
func (p *Player) ForceMakeFriendRequest(req *sp.ForceMakeFriendRequest) (res *sp.ForceMakeFriendResponse) {
	// 该协议只会返回data.Errcode.UserNotFound或data.Errcode.ServerErr（已经是好友关系）
	// 通过此协议添加的好友，都是传入accountId/uid
	var idType int64 = req.IdType //0-accountId, 1-uid, 2-identifId
	// 不能添加自己为好友
	if req.FriendID == "" || req.FriendID == p.User.Uid.Hex() {
		err := errors.NewErrcode(data.Errcode.UserNotFound, "不能添加自己为好友,或好友ID为空")
		res = &sp.ForceMakeFriendResponse{
			Errcode: errors.ErrCode(err),
		}
		return res
	}
	switch idType {
	case 0: // accountId 交友
		// 根据AccountId找到对应的UID
		// 参考game/player/player.go CpLoginRequest
		uid, err := lib.FindUidByAccountId(req.FriendID)
		if err != nil {
			err := errors.NewErrcode(data.Errcode.UserNotFound, "没有找到用户")
			res = &sp.ForceMakeFriendResponse{
				Errcode: errors.ErrCode(err),
			}
			return res
		}
		err = lib.MakeFriendsWithPlayer(p.User, uid)
		if err != nil {
			res = &sp.ForceMakeFriendResponse{
				Errcode: errors.ErrCode(err),
			}
			return res
		}
		fData := p.User.GetUserFriendsData()
		res = &sp.ForceMakeFriendResponse{
			Errcode:     0,
			FriendsData: lib.FormatUserFriendsData(fData),
		}
		return res
	case 1: // uid
		err := lib.MakeFriendsWithPlayer(p.User, req.FriendID)
		if err != nil {
			res = &sp.ForceMakeFriendResponse{
				Errcode: errors.ErrCode(err),
			}
			return res
		}
		fData := p.User.GetUserFriendsData()
		res = &sp.ForceMakeFriendResponse{
			Errcode:     0,
			FriendsData: lib.FormatUserFriendsData(fData),
		}
		return res
	case 2: // IdentifId 交友
		uid, err := lib.FindUidByIdentifId(req.FriendID)
		if err != nil {
			res = &sp.ForceMakeFriendResponse{
				Errcode: errors.ErrCode(err),
			}
			return
		}
		err = lib.MakeFriendsWithPlayer(p.User, uid)
		if err != nil {
			res = &sp.ForceMakeFriendResponse{
				Errcode: errors.ErrCode(err),
			}
			return
		}
		fData := p.User.GetUserFriendsData()
		res = &sp.ForceMakeFriendResponse{
			FriendsData: lib.FormatUserFriendsData(fData),
		}
		return
	default:
		err := errors.NewErrcode(data.Errcode.UserNotFound, "没有该ID类型的好友")
		res = &sp.ForceMakeFriendResponse{
			Errcode: errors.ErrCode(err),
		}
		return res
	}
}

// 12006 请求交朋友
func (p *Player) ReqMakeFriendRequest(req *sp.ReqMakeFriendRequest) (res *sp.ReqMakeFriendResponse) {
	var idType int64 = req.IdType //0-accountId, 1-uid
	fData := p.User.GetUserFriendsData()
	// 不能添加自己为好友
	if req.FriendID == "" || req.FriendID == p.User.Uid.Hex() {
		err := errors.NewErrcode(data.Errcode.UserNotFound, "不能添加自己为好友,或好友ID为空") // 1001
		res = &sp.ReqMakeFriendResponse{
			Errcode:     errors.ErrCode(err),
			FriendsData: lib.FormatUserFriendsData(fData),
		}
		return res
	}
	if fData.DailyReqFriendNum > 20 {
		err := errors.NewErrcode(data.Errcode.ServerErr, "超过当天发起申请好友上限") // 999
		res = &sp.ReqMakeFriendResponse{
			Errcode:     errors.ErrCode(err),
			FriendsData: lib.FormatUserFriendsData(fData),
		}
		return res
	}
	switch idType {
	case 0: // accountId 交友
		// 根据AccountId找到对应的UID
		// 参考game/player/player.go CpLoginRequest
		uid, err := lib.FindUidByAccountId(req.FriendID)
		fData := p.User.GetUserFriendsData()
		if err != nil {
			err := errors.NewErrcode(data.Errcode.UserNotFound, "没有找到用户") // 1001
			res = &sp.ReqMakeFriendResponse{
				Errcode:     errors.ErrCode(err),
				FriendsData: lib.FormatUserFriendsData(fData),
			}
			return res
		}
		err = lib.ReqMakeFriendsWithPlayer(p.User, uid)
		fData = p.User.GetUserFriendsData()
		if err != nil {
			res = &sp.ReqMakeFriendResponse{
				Errcode:     errors.ErrCode(err),
				FriendsData: lib.FormatUserFriendsData(fData),
			}
			return res
		}
		res = &sp.ReqMakeFriendResponse{
			Errcode:     0,
			FriendsData: lib.FormatUserFriendsData(fData),
		}
		return res
	case 1: // uid
		err := lib.ReqMakeFriendsWithPlayer(p.User, req.FriendID)
		fData := p.User.GetUserFriendsData()
		if err != nil {
			res = &sp.ReqMakeFriendResponse{
				Errcode:     errors.ErrCode(err),
				FriendsData: lib.FormatUserFriendsData(fData),
			}
			return res
		}
		res = &sp.ReqMakeFriendResponse{
			Errcode:     0,
			FriendsData: lib.FormatUserFriendsData(fData),
		}
		return res
	case 2: // IdentifId 交友
		uid, err := lib.FindUidByIdentifId(req.FriendID)
		fData := p.User.GetUserFriendsData()
		if err != nil {
			res = &sp.ReqMakeFriendResponse{
				Errcode:     errors.ErrCode(err),
				FriendsData: lib.FormatUserFriendsData(fData),
			}
			return
		}
		err = lib.ReqMakeFriendsWithPlayer(p.User, uid)
		fData = p.User.GetUserFriendsData()
		if err != nil {
			res = &sp.ReqMakeFriendResponse{
				Errcode:     errors.ErrCode(err),
				FriendsData: lib.FormatUserFriendsData(fData),
			}
			return
		}
		res = &sp.ReqMakeFriendResponse{
			Errcode:     0,
			FriendsData: lib.FormatUserFriendsData(fData),
		}
		return
	default:
		err := errors.NewErrcode(data.Errcode.UserNotFound, "没有该ID类型的好友") // 1001
		fData := p.User.GetUserFriendsData()
		res = &sp.ReqMakeFriendResponse{
			Errcode:     errors.ErrCode(err),
			FriendsData: lib.FormatUserFriendsData(fData),
		}
		return res
	}
}

// 12007 答复好友请求
func (p *Player) DuelReqFriendRequest(req *sp.DuelReqFriendRequest) (res *sp.DuelReqFriendResponse) {
	fData := p.User.GetUserFriendsData() // 获取玩家好友信息
	_, find := fData.ReqFriendsLst[req.FriendUID]
	if find == false {
		// 不在申请列表中
		err := errors.NewErrcode(data.Errcode.UserNotFound, "玩家不在申请列表中")
		res = &sp.DuelReqFriendResponse{
			Errcode: errors.ErrCode(err),
		}
		return res
	}
	if req.Answer == 0 {
		// 拒绝
		lib.ReqFriends_Refuse(p.User, req.FriendUID)
	} else if req.Answer == 1 {
		// 接受
		lib.ReqFriends_Accept(p.User, req.FriendUID)
	}
	fData = p.User.GetUserFriendsData() // 再次获取玩家好友信息
	res = &sp.DuelReqFriendResponse{
		Errcode:       0,
		FriendsData:   lib.FormatUserFriendsData(fData),
		ReqFriendsLst: lib.FormatSimpleFriendsDat(fData.ReqFriendsLst),
	}
	return res
}

// 12008 获取好友请求列表
func (p *Player) GetReqFriendsLstRequest(req *sp.GetReqFriendsLstRequest) (res *sp.GetReqFriendsLstResponse) {
	fData := p.User.GetUserFriendsData() // 获取玩家好友信息
	res = &sp.GetReqFriendsLstResponse{
		Errcode:     0,
		FriendsData: lib.FormatSimpleFriendsDat(fData.ReqFriendsLst),
	}
	return
}

// 12009 主动删除好友
func (p *Player) DeleteFriendRequest(req *sp.DeleteFriendRequest) (res *sp.DeleteFriendResponse) {
	targetUID := req.FriendUID
	fData := p.User.GetUserFriendsData() // 获取玩家好友信息
	_, find := fData.FriendsLst[targetUID]
	if find {
		lib.DeleteFriends(p.User, targetUID)
		fData = p.User.GetUserFriendsData() // 获取玩家好友信息
		res = &sp.DeleteFriendResponse{
			Errcode:     0,
			FriendsData: lib.FormatUserFriendsData(fData),
		}
		return
	} else {
		err := errors.NewErrcode(data.Errcode.UserNotFound, "玩家不在好友列表中")
		res = &sp.DeleteFriendResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
}

// 12012 玩家分享游戏，获取分享码
func (p *Player) ShareGameRequest(req *sp.ShareGameRequest) (res *sp.ShareGameResponse) {
	code, err := lib.GetShareCode(p.User)
	if err != nil {
		res = &sp.ShareGameResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	res = &sp.ShareGameResponse{
		ShareCode: code,
	}
	return
}

// 12013 获取分享码的道具
func (p *Player) ShareCodePropRequest(req *sp.ShareCodePropRequest) (res *sp.ShareCodePropResponse) {
	uProp, resGoods, boxResList, tuUid, mail, err := lib.GetShareCodeProp("ShareCodePropRequest", p.User, req.ShareCode, p.Send)
	if err != nil {
		res = &sp.ShareCodePropResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}

	res = &sp.ShareCodePropResponse{
		Res:    lib.FormatGoodsInfoSps(resGoods),
		BoxRes: lib.FormatBoxResSps(boxResList),
	}

	p.TimelyUpdateProp(req.GetType(), uProp)
	utils.AsyncFunc(func() {
		p.UpdateProp(req.GetType(), uProp)
	})
	tuPlayer, ok := p.getPlayer(tuUid)
	if ok {
		utils.AsyncFunc(func() {
			tuPlayer.Send(&sp.MailNoticeRequest{MailData: lib.FormatMailSp(mail)})
		})
	}

	return
}
